package com.sky.common.utils;

import io.minio.BucketExistsArgs;
import io.minio.MinioClient;
import io.minio.UploadObjectArgs;
import io.minio.errors.MinioException;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

public class MinIOUploader {

    private static MinioClient minioClient;

    public static void main(String[] args) {
        // MinIO 服务器配置
        String minioEndpoint = "http://172.22.2.241:9000"; // MinIO服务器地址
        String accessKey = "wdetdPAvHITLE6JlyLDV";         // Access Key
        String secretKey = "vpZ9mr09oSICRIoK5Yx3zkcer6YKWCAZTUos8l0t";                 // 替换为你的SecretKey
        String bucketName = "test-images";                 // Bucket名称
        String objectName = "1.mp4";                       // 上传后的对象名
        String filePath = "D:\\VideoDownload\\1.mp4";                 // 本地文件路径

        try {
            // 1. 创建MinIO客户端
            MinioClient minioClient = MinioClient.builder()
                    .endpoint(minioEndpoint)
                    .credentials(accessKey, secretKey)
                    .build();

            // 2. 检查Bucket是否存在
            boolean found = minioClient.bucketExists(BucketExistsArgs.builder()
                    .bucket(bucketName)
                    .build());
            if (!found) {
                System.err.println("Bucket不存在: " + bucketName);
                return;
            }

            // 3. 上传文件到MinIO
            minioClient.uploadObject(
                    UploadObjectArgs.builder()
                            .bucket(bucketName)
                            .object(objectName)
                            .filename(filePath)
                            .build());

            System.out.println("文件上传成功！");
            System.out.println("访问URL: " + minioEndpoint + "/" + bucketName + "/" + objectName);

        } catch (MinioException e) {
            System.err.println("MinIO错误: " + e.getMessage());
        } catch (IOException | InvalidKeyException | NoSuchAlgorithmException e) {
            System.err.println("其他错误: " + e.getMessage());
        }
    }

    // 1. 创建MinIO客户端
    public static MinioClient getInstance() {
        if (minioClient == null) {
            synchronized (MinIOUploader.class) {
                if (minioClient == null) {
                    minioClient = MinioClient.builder()
                            .endpoint(MinioConstants.MINIO_HOST)
                            .credentials(MinioConstants.AK, MinioConstants.SK)
                            .build();
                }
            }
        }
        return minioClient;
    }

    public static String uploadFile(String bucketName, String objectName, String filePath) {
        MinioClient minioClient = getInstance();
        try {
            // 2. 检查Bucket是否存在
            boolean found = minioClient.bucketExists(BucketExistsArgs.builder()
                    .bucket(bucketName)
                    .build());
            if (!found) {
                System.err.println("Bucket不存在: " + bucketName);
                return null;
            }

            // 3. 上传文件到MinIO
            getInstance().uploadObject(
                    UploadObjectArgs.builder()
                            .bucket(bucketName)
                            .object(objectName)
                            .filename(filePath)
                            .build());
            return MinioConstants.MINIO_HOST + "/" + bucketName + "/" + objectName;
        } catch (MinioException e) {
            System.err.println("MinIO错误: " + e.getMessage());
        } catch (IOException | InvalidKeyException | NoSuchAlgorithmException e) {
            System.err.println("其他错误: " + e.getMessage());
        }
        return null;
    }
}
